<?php
namespace app\api\controller;

use app\api\controller\Authbase;
use app\api\model\BlessingModel;
use think\Controller;

/**
 * 祝福接口
 */
class Blessing extends Authbase
{
    /**
     * 祝福首页
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function index() {
        $page = input('page/d', 1);
        $model = new BlessingModel();
        $list = $model->cateByPage($page);
        if ($list === false) {
            return json(['code' => 0, 'msg' => $model::$error_msg]);
        }
        return json(['code' => 1, 'msg' => 'success', 'data' => $list]);
    }

    /**
     * 祝福详情
     *
     */
    public function view() {
        $blessing_id = input('blessing_id/d', '');
        if (empty($blessing_id)) {
            return json(['code' => 0, 'error' => '请传递参数blessing_id']);
        }
        $blessingM = new BlessingModel();
        $blessing = $blessingM->detail($blessing_id, $this->Auth_Wxuser_name);
        return json(['code' => 1, 'msg' => 'success', 'data' => $blessing]);
    }

    /**
     * 点击事件
     *
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function click() {
        $id = input('id/d', '');
        if(empty($id)) {
            return json(['code' => 0, 'error' => '请传递id']);
        }
        $blessingM = new BlessingModel();
        if (!$res = $blessingM->click($id)) {
            return json(['code' => 0, 'error' => BlessingModel::$error_msg?BlessingModel::$error_msg:$blessingM->getError()]);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 分享事件
     * @return \think\response\Json
     */
    public function share() {
        $id = input('id/d', '');
        if(empty($id)) {
            return json(['code' => 0, 'error' => '请传递id']);
        }
        $blessingM = new BlessingModel();
        if (!$blessingM->share($id)) {
            return json(['code' => 0, 'error' => BlessingModel::$error_msg?BlessingModel::$error_msg:$blessingM->getError()]);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }
}